package com.handpay.zztong.hp.webpay.model;

import android.content.DialogInterface;

import com.handpay.framework.LuaTableUtil;
import com.handpay.framework.fastjson.FastJsonHelper;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.base.callback.BusinessEngineListener;
import com.handpay.zztong.hp.base.model.base.BaseModel;
import com.handpay.zztong.hp.bean.Bill;
import com.handpay.zztong.hp.config.HttpUrlConstant;

import java.io.UnsupportedEncodingException;
import java.util.Hashtable;

import se.krka.kahlua.vm.LuaTable;

/**
 * @author sxshi on 2017/4/5.
 *         email:emotiona_xiaoshi@126.com
 *         describe:Describe the function  of the current class
 */

public class WebPayModel extends BaseModel {
    private BusinessEngineListener<Bill> businessEngineListener;
    private ZZTongActivity zzTongActivity;

    public boolean doWalletAccountTransfer(ZZTongActivity activity, Hashtable<String, String> params, BusinessEngineListener<Bill> listener) {
        this.businessEngineListener = listener;
        this.zzTongActivity = activity;
        return doPost(activity, HttpUrlConstant.Do_WalletAccountTransfer, params, true, true);
    }

    @Override
    public boolean netResponse(String action, byte[] data) {
        if (super.netResponse(action, data, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                zzTongActivity.setResult(0);
                zzTongActivity.finish();
            }
        })) {
            return true;
        } else if (action.equals(HttpUrlConstant.Do_WalletAccountTransfer)) {
            AccountTransfer accountTransfer;
            Bill bill = new Bill();
            if (data_type == DATA_TYPE.DATA_TYPE_JSON) {
                try {
                    accountTransfer = FastJsonHelper.getObject(new String(data, "UTF-8"), AccountTransfer.class);
                    bill = CommonUtils.generateBill(accountTransfer, Bill.APPCODE.TRANS);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            } else {
                LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, true);
                bill = CommonUtils.generateBill(luaTable, Bill.APPCODE.TRANS);
            }
            if (businessEngineListener != null) businessEngineListener.callbackBusinessResponse(bill);
        }
        return true;
    }
}
